var globalCount = 0;

$(".top").click(function() {
  $('body,html').animate({
    scrollTop : 0
  }, 1000);
  return false;
});

$(function() {
  //查找h1-h6  
  var i = 1;
  var navToc = "";
  var navToc = "";
  $(".news_infos :header").each(function() {
    navToc += '<li class="nav-item nav-level-4" id="#nav1_' + i + '"><a class="nav-link" ><span class="nav-number">' + i + '.</span> <span class="nav-text">' + $(this).text() + '</span></a></li>';
    $("#nav1_" + i).css({
      'border-left' : '5px solid #FF926F',
      'background-color' : ' #f6f6f6',
      padding : '7px 20px',
      margin: '12px 0'
    });
    i++;
  });
  $(".nav1").html(navToc);
});
var count = 1;
var width = window.innerWidth || document.documentElement.clientWidth;
//评论
/*(function() {
  var appid = 'cytzg9rLH';
  var conf = 'prod_230eb23e872ad7a4302e5802e6f91bf9';
  if (width < 960) {
    window.document.write('<script id="changyan_mobile_js" charset="utf-8" type="text/javascript" src="https://changyan.sohu.com/upload/mobile/wap-js/changyan_mobile.js?client_id=' + appid + '&conf=' + conf + '"><\/script>');
  } else {
    var loadJs = function(d, a) {
      var c = document.getElementsByTagName("head")[0] || document.head || document.documentElement;
      var b = document.createElement("script");
      b.setAttribute("type", "text/javascript");b.setAttribute("charset", "UTF-8");b.setAttribute("src", d);
      if (typeof a === "function") {
        if (window.attachEvent) {
          b.onreadystatechange = function() {
            var e = b.readyState;
            if (e === "loaded" || e === "complete") {
              b.onreadystatechange = null;a()
            }
          }
        } else {
          b.onload = a
        }
      }
      c.appendChild(b)
    };
    loadJs("https://changyan.sohu.com/upload/changyan.js", function() {
      window.changyan.api.config({
        appid : appid,
        conf : conf
      })
    });
  }
})();*/

if (width < 700) {
  $(".tool-box").css("display", "none");
}
$(window).scroll(function() {
  if ($(document).scrollTop() > 20 && count == 1) {
    $(".dj").css("display", "block");
    initBlogByClick();
    count++;
  }
  if ($(document).scrollTop() > 1200 && width > 700) {
    $(".top").addClass('cd-is-visible fadeIn');
    $(".fixed-menu-list").css("display", "block");
  } else {
    $(".top").removeClass('cd-is-visible fadeOut');
    $(".fixed-menu-list").css("display", "none");
  }
});

var ftf = 404;
var tag = '';
$(document).ready(function() {

  initBlogById();
  if (ftf != 404) {
	 Tags();
	 selectPrevBlog();
	 selectNextBlog();
	 initBlogByRel(); //初始化相关文章
  }else{
	 var str = '';
	 str = '<h1 style="font-size:110px;text-align:center;margin:20px;">404' + '</h1>' 
	 		+ '<h3 style="text-align:center;" class="font-bold">抱歉，你所访问的页面不存在~</h3>' 
	 		+ '<h4 style="margin-top:55px;text-align:center;">'
	 		+ '<a style="background-color: #676a6c;padding: 5px 10px;color: #fff;border-radius: 10px;" href="${pageContext.request.contextPath}/index.jsp">去首页</a> </h4>'
	 // 初始化数据
	 $("#flag").html(str);
  }
  
  initBlogByLike();
  setTimeout(function() {
    $(".ds").css("opacity", "1");
  }, 1000);
  var topId = $(".nav-item"); /*获取目录点击区域*/
  topId.click(function() {
    var topId = $(this).attr("id");
    $('body,html').animate({
      scrollTop : $(topId).offset().top
    }, 800);
    return false;
  });
  
});
//初始化文章数据
var initBlogById = function() {
	var url = window.location.href;
	var url_param = url.split("=")[1];

	//编辑博客
	var param = {
		blogId : url_param
	}
	$.ajax({
		url : '/blogadmin/getblogbyid',
		type : 'post',
		async: false,
		data : param,
		dataType : 'json',
		success : function(data) {
			//查询成功
			if (data.success) {
				$("#blogerName").html(data.blog.bloger.blogerName);
				$("#blogId").val(data.blog.blogId);
				$("#blogTitle").html(data.blog.blogTitle);
				$("#blogIntroduction").html(data.blog.blogIntroduction);
				$("#blogTypeId").val(data.blog.blogType.blogTypeId);
				$("#blogContent").html(data.blog.blogContent);
				$(".tag").val(data.blog.keyWord);
				$("title").html(data.blog.blogTitle);
				//加载编辑页面预览的  时间和浏览量
				$(".newsview").find(".au01").html(data.blog.bloger.blogerName);
				$(".newsview").find(".au02").html(Format(data.blog.createTime, "yyyy-MM-dd"));
				$(".au03").find('b').html(data.blog.clickNum);
				$("#commentNum").html(data.blog.commentNum);
				
				$("#clicknum").html(data.blog.clickNum);
				$("#commentNum").html(data.blog.commentNum);
				ftf = 200;
				//增加点击量
				var sum = data.blog.clickNum;
				var bid = data.blog.blogId;
				sum++;
				$.ajax({
					url : '../blogadmin/modifyblogclicknum',
					type : 'post',
					data : {
						blogId : bid,
						clickNum : sum,
						commentNum : 0,
						agreeNum : 0,
						isTop : 0,
						isRecommend : 0,
						status : 0,
						blogTypeId : 0
					},
					dataType : 'json'
				});
			}
			
		},
		error : function() {
			ftf = 404;
			swal("查询错误", "请重试操作", "error");
		}
	});
}
	
var Tags = function() {
  var tag = $(".tag").val();
  var keyword = "";
  $(".newsview").find(".tags").html("");
  if (tag != '' && tag != null) {
    if (tag.search(';') != -1) {
      var strs = new Array();
      strs = tag.split(";");
      for (var i = 0; i < strs.length && strs[i] != ''; i++) {
        keyword += '<a href="#">' + strs[i] + '</a>';
      }
    } else {
      keyword = '<a href="#">' + tag + '</a>';
    }
  }
  $(".newsview").find(".tags").append(keyword);
}

var selectPrevBlog = function() {
  var url = window.location.href;
  var url_param = url.split("=")[1];
  var params = {
    blogId : parseInt(url_param) - parseInt(1)
  };
  $.ajax({
	    url : '../blogadmin/getblogbyidpre',
	    type : 'post',
	    data : params,
	    dataType : 'json',
	    success : function(data) {
	      var preTitle = '';
	      if (data.success) {
	    	  preTitle = '<a href="../frontend/info.html?blogId=' + data.blog.blogId + '">' + data.blog.blogTitle + '</a>';
	      } else {
	    	  preTitle = '<span>无</span>';
	      }
	      $(".pre").html(preTitle);
	    },
	    error : function() {
	      layer.msg('加载的太快啦', {
	        icon : 2
	      });
	    }
  });
};

var selectNextBlog = function() {
  var url = window.location.href;
  var url_param = url.split("=")[1];
  var params = {
	blogId : parseInt(url_param) + parseInt(1)
  };
  $.ajax({
    url : '../blogadmin/getblogbyidnext',
    type : 'post',
    data : params,
    dataType : 'json',
    success : function(data) {
      var nextTitle = '';
      if (data.success) {
        nextTitle = '<a href="../frontend/info.html?blogId=' + data.blog.blogId + '">' + data.blog.blogTitle + '</a>';
      } else {
        nextTitle = '<span>无</span>';
      }
      $(".next").html(nextTitle);
    },
    error : function() {
      layer.msg('加载的太快啦', {
        icon : 2
      });
    }
  });

};

//初始化相关文章
var initBlogByRel = function() {
  var params = {
    pageSize : 6,
    pageIndex : 1,
    blogTypeId : $("#blogTypeId").val(),
    isTop : 0,
    isRecommend : 0,
    status : 1
  };
  $.ajax({
    url : '../blogadmin/getbloglistbypage',
    type : 'get',
    data : params,
    dataType : 'json',
    success : function(data) {
      var relBlog = '';
      var data = data.blogList;
      for (var i = 0; i < data.length; i++) {
        relBlog += '<li><i style="color:#5788aa" class="fa fa-paperclip"></i> <a href="../frontend/info.html?blogId=' + data[i].blogId + '" title="' + data[i].blogTitle + '">' + data[i].blogTitle + '</a></li>'
      }
      // 初始化数据
      $(".otherlink").find("ul").html(relBlog);
    },
    error : function() {
      layer.msg('加载的太快啦', {
        icon : 2
      });
    }
  });
};

//初始化推荐
var initBlogByLike = function() {
  //设置参数
  var params = {
    pageSize : 5,
    pageIndex : 1,
    isrecommend : 1, //1 表示推荐
    blogTypeId : 0,
    isTop : 0,
    status : 1,
    agreeNum : 0,
    commentNum : 0,
    clickNum : 0
  };
  $.ajax({
    url : '../blogadmin/getbloglistbypage',
    type : 'get',
    data : params,
    dataType : 'json',
    success : function(data) {
      var likeBlog = '';
      var data = data.blogList;
      var time = '';
      for (var i = 0; i < data.length; i++) {
        time = i * 0.05;
        likeBlog += '<li style="animation-delay:0.' + i + 's" class="animated fadeIn"><b><a href="../frontend/info.html?blogId=' + data[i].blogId + '">'
          + data[i].blogTitle
          + '</a></b><p><i><img src="' + data[i].blogImage + '"></i><span>'
          + data[i].blogIntroduction + '<span></p></li>'
      }
      // 初始化数据
      $(".paihang").find(".like").html(likeBlog);
    },
    error : function() {
      layer.msg('加载的太快啦', {
        icon : 2
      });
    }
  });
};

//初始化点击排行
var initBlogByClick = function() {
  //设置参数
  var params = {
    pageSize : 5,
    pageIndex : 1,
    status : 1,
    isTop : 0,
    isRecommend : 0,
    blogTypeId : 0,
    agreeNum : 0,
    commentNum : 0,
    clickNum : 0
  };
  $.ajax({
    url : '../blogadmin/getbloglistbypageclick',
    type : 'get',
    data : params,
    dataType : 'json',
    success : function(data) {
      var clickBlog = '';
      var data = data.blogList;
      var time = '';
      for (var i = 0; i < data.length; i++) {
        time = i * 0.05;
        clickBlog += '<li style="animation-delay:0.' + i + 's" class="animated fadeIn"><b><a href="../frontend/info.html?blogId=' + data[i].blogId + '">'
          + data[i].blogTitle
          + '</a></b><p><i><img src="' + data[i].blogImage + '"></i><span>'
          + data[i].blogIntroduction + '</span></p></li>'
      }
      // 初始化数据
      $(".paihang").find(".click").html(clickBlog);
    },
    error : function() {
      layer.msg('加载的太快啦', {
        icon : 2
      });
    }
  });
};


//格式化时间
function Format(datetime, fmt) {
  if (parseInt(datetime) == datetime) {
    if (datetime.length == 10) {
      datetime = parseInt(datetime) * 1000;
    } else if (datetime.length == 13) {
      datetime = parseInt(datetime);
    }
  }
  datetime = new Date(datetime);
  var o = {
    "M+" : datetime.getMonth() + 1, //月份   
    "d+" : datetime.getDate(), //日   
    "h+" : datetime.getHours(), //小时   
    "m+" : datetime.getMinutes(), //分   
    "s+" : datetime.getSeconds(), //秒   
    "q+" : Math.floor((datetime.getMonth() + 3) / 3), //季度   
    "S" : datetime.getMilliseconds()
  //毫秒   
  };
  if (/(y+)/.test(fmt))
    fmt = fmt.replace(RegExp.$1, (datetime.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt))
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
}